package 双指针.普通双指针;

import java.util.Arrays;

public class 最短无序连续子数组_581 {

    public int findUnsortedSubarray(int[] nums) {

        int n = nums.length;
        if (n == 1) {
            return 0;
        }

        int[] numsSort = nums.clone();
        Arrays.sort(numsSort);
        int left = Integer.MAX_VALUE, right = 0;
        for (int i = 0; i < n; i++) {
            if (numsSort[i] != nums[i]) {
                left = i;
                break;
            }
        }
        for (int i = n - 1; i >= 0; i--) {
            if (numsSort[i] != nums[i]) {
                right = i;
                break;
            }
        }
        if (right - left >= 0) {
            return right - left + 1;
        } else {
            return 0;
        }
    }
}
